package cn.com.dg

import cn.com.callback.ServiceBackInvoke
import cn.com.elite.EliteBean
import cn.com.elite.Util
import cn.com.kh.KHDBConfig








import net.sf.json.JSONArray
import net.sf.json.JSONObject
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.springframework.jdbc.core.JdbcTemplate

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse


/**
* @Title:同步乐购乐销客户数据
* @Description:⽤户数据同步/API
* @Param:
* @return:
* @Author: TaoHao
* @Date: 2023/6/30
*/
public class autoSyncLgCustomer implements ServiceBackInvoke {

    private static final Log log = LogFactory.getLog(autoSyncLgCustomer.class);
    @Override
    String doCallBack(EliteBean eliteBean, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        try {
            long startTime = System.currentTimeMillis();
            JdbcTemplate jt = new JdbcTemplate(eliteBean.getDataSource());

            // 1.调用乐购乐销同步客户接口
            String CustomerGetStr = Util.doHttpGet(KHDBConfig.getAPIURL(eliteBean, "LGLX_Customer")+"size=20&page=1", "UTF-8", 5000, 0, "");
            log.debug("【同步乐购乐销⽤户数据同步/API】准备处理的JSON" + CustomerGetStr.toString());

            JSONObject json =JSONObject.fromObject(CustomerGetStr);
            int totalPages= json.getString("totalPages") as int;
            log.debug("【同步乐购乐销⽤户数据同步/API】获取到总分页数量":+totalPages);




            if (totalPages>0){
                //遍历分页数
            for(int i=1;i<totalPages;i++){


                log.debug("【同步乐购乐销⽤户数据同步/API】准备调用的页："+i);
                //遍历调用接口分页数获取数据
                String ListGetStr = Util.doHttpGet(KHDBConfig.getAPIURL(eliteBean, "LGLX_Customer")+"size=20&page="+i, "UTF-8", 5000, 0, "");

                JSONObject json2 =JSONObject.fromObject(ListGetStr);
                //获取到json数组
                JSONArray dataArr = json2.getJSONArray("content");
                if (dataArr.size() > 0) {
                    for (int b = 0; b < dataArr.size(); b++) {
                        JSONObject dataJsonTwo = dataArr.getJSONObject(b);
                        String id= dataJsonTwo.getString("id") //客户ID
                        String code= dataJsonTwo.getString("code") //客户编码
                        String name= dataJsonTwo.getString("name") //客户名称
                        String marketAreaId= dataJsonTwo.getString("marketAreaId") //市场区域ID
                        String marketAreaCode= dataJsonTwo.getString("marketAreaCode") //市场区域编码
                        String marketAreaName= dataJsonTwo.getString("marketAreaName") //市场区域名称
                        String customerCategoryId= dataJsonTwo.getString("customerCategoryId") //客户分类ID
                        String customerCategoryCode= dataJsonTwo.getString("customerCategoryCode") //客户分类编码
                        String customerCategoryName= dataJsonTwo.getString("customerCategoryName") //客户分类名称
                        String channelTypeId= dataJsonTwo.getString("channelTypeId") //渠道类型ID
                        String channelTypeCode= ""; //渠道类型编码dataJsonTwo.getString("channelTypeCode")
                        String channelTypeName= dataJsonTwo.getString("channelTypeName") //渠道类型名称
                        String countryId= ""; //国家   IDdataJsonTwo.getString("countryId")
                        String countryCode= "";//国家编码 dataJsonTwo.getString("countryCode")
                        String  countryName="" //国家名称  dataJsonTwo.getString("countryName")
                        String provinceId= dataJsonTwo.getString("provinceId") //省份ID
                        String provinceName= dataJsonTwo.getString("provinceName") //省份名称
                        String provinceCode= dataJsonTwo.getString("provinceCode") //省份编码
                        String  cityId= dataJsonTwo.getString("cityId") //城市ID
                        String  cityCode= dataJsonTwo.getString("cityCode") //城市编码
                        String  cityName= dataJsonTwo.getString("cityName") //城市名称
                        String countyId= dataJsonTwo.getString("countyId") //区域ID
                        String countyName= dataJsonTwo.getString("countyName") //区域名称
                        String  countyCode= dataJsonTwo.getString("countyCode") //区域编码
                        String  detailAddr= dataJsonTwo.getString("detailAddr") //详细地址
                        String  tel= dataJsonTwo.getString("tel") == null ? '' : dataJsonTwo.getString("tel");//联系方式

                        //判断ID是否存在 不存在新增  存在更新
                        String isString = '''select Customer_guid from customer  where Customer_guid= ? ''';
                        List<Map<String, Object>> selelctList = jt.queryForList(isString, id);


                        if (selelctList.size()>0){
                            String UpdateSql='''
                                           Update
                                           customer  
                                           Set
                                            STRINGFIELD12= ?,
                                            name= ?,
                                            marketAreaId= ?,
                                            marketAreaCode=?,
                                            marketAreaName= ?,
                                            customerCategoryId= ?,
                                            customerCategoryCode= ?,
                                            customerCategoryName= ?,
                                            channelTypeId= ?,
                                            channelTypeCode= ?,
                                            channelTypeName= ?,
                                            countryId= ?,
                                            countryCode= ?,
                                            countryName= ?,
                                            provinceId= ?,
                                            provinceName= ?,
                                            provinceCode= ?,
                                            cityId= ?,
                                            cityCode= ?,
                                            cityName= ?,
                                            countyId= ?,
                                            countyName= ?,
                                            countyCode= ?,
                                            detailAddr= ?,
                                            tel= ?,
                                            sync_time=now()
                                            where STRINGFIELD18=?
                                          ''';
                            jt.update(UpdateSql,
                                    code,
                                    name,
                                    marketAreaId,
                                    marketAreaCode,
                                    marketAreaName,
                                    customerCategoryId,
                                    customerCategoryCode,
                                    customerCategoryName,
                                    channelTypeId,
                                    channelTypeCode,
                                    channelTypeName,
                                    countryId,
                                    countryCode,
                                    countryName,
                                    provinceId,
                                    provinceName,
                                    provinceCode,
                                    cityId,
                                    cityCode,
                                    cityName,
                                    countyId,
                                    countyName,
                                    countyCode,
                                    detailAddr,
                                    tel,
                                    id
                            );



                        }else{

                            String insertSql='''
                                 insert into  lglx_customer
                                 (
                                           rec_guid,
                                            id,
                                             code,
                                            name,
                                            marketAreaId,
                                            marketAreaCode,
                                            marketAreaName,
                                            customerCategoryId,
                                            customerCategoryCode,
                                            customerCategoryName,
                                            channelTypeId,
                                            channelTypeCode,
                                            channelTypeName,
                                            countryId,
                                            countryCode,
                                             countryName,
                                            provinceId,
                                            provinceName,
                                            provinceCode,
                                            cityId,
                                            cityCode,
                                            cityName,
                                            countyId,
                                            countyName,
                                             countyCode,
                                             detailAddr,
                                             tel,
                                             sync_time,
                                             createddate
                                 )values(
                                 uuid(),
                                ?,
                                 ?,
                                  ?,
                                   ?,
                                     ?,
                                       ?,
                                         ?,
                                           ?,
                                             ?,
                                               ?,
                                                 ?,
                                                   ?,
                                                     ?,
                                                       ?,
                                                         ?,
                                                           ?,
                                                             ?,
                                                               ?,
                                                                 ?,
                                                                   ?,
                                                                     ?,
                                                                       ?,
                                                                         ?,
                                                                           ?,
                                                                             ?,
                                                                               ?,
                                                                                 now(),
                                                                                    now()
                                 )
                                    ''';
                            jt.update(insertSql,
                                    id,
                                    code,
                                    name,
                                    marketAreaId,
                                    marketAreaCode,
                                    marketAreaName,
                                    customerCategoryId,
                                    customerCategoryCode,
                                    customerCategoryName,
                                    channelTypeId,
                                    channelTypeCode,
                                    channelTypeName,
                                    countryId,
                                    countryCode,
                                    countryName,
                                    provinceId,
                                    provinceName,
                                    provinceCode,
                                    cityId,
                                    cityCode,
                                    cityName,
                                    countyId,
                                    countyName,
                                    countyCode,
                                    detailAddr,
                                    tel);
                        }

                    }
                }



            }






            }

        long endTime = System.currentTimeMillis();
        log.debug("【同步乐购乐销客户数据同步数据耗时】:" + (endTime - startTime));
    } catch (Exception e) {
            log.error("【同步乐购乐销客户数据同步数据异常】：" + e);
        }

        return null
    }
}
